﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using WP7PhoneTracker.Commanding;
using WP7PhoneTracker.EntityClasses;
using WP7PhoneTracker.Helpers;

namespace WP7PhoneTracker.Model
{
    public class TrakingName_Model : INotifyPropertyChanged
    {
        #region NotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        public TrakingName_Model()
        {
            #region Commanding

            DeleteCommand = new DelegateCommand(btnDelete, CanDelete);

            SaveCommand = new DelegateCommand(btnSave, CanSave);

            SelectCommand = new DelegateCommand(btnSelect, CanSelect);

            #endregion

            TrackingItemNameError = Visibility.Collapsed;

            BindTrackingNameItems();
        }

        #region Functions
        
        private void NavigateTo(String url)
        {
            App.Current.NavigateTo(url);
            SelectedTrackingName = null;
        }

        public void BindTrackingNameItems()
        {
            var result = GetTrackingNameItems();
            if (result != null) TrackingNames = new ObservableCollection<TrackingName>(result);
        }

        public IEnumerable<TrackingName> GetTrackingNameItems()
        {
            try
            {
                return CrudTrackingName.GetTrackingNames().OrderByDescending(res => res.DtRegister).ToList();
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                return null;
            }
        }


        #endregion

        #region Properties

        private ObservableCollection<TrackingName> _trackingNames;
        public ObservableCollection<TrackingName> TrackingNames
        {
            get { return _trackingNames?? (_trackingNames = new ObservableCollection<TrackingName>()); }
            set
            {
                _trackingNames = value;
                NotifyPropertyChanged("TrackingNames");
            }
        }

        private TrackingName _selectedTrackingName;
        public TrackingName SelectedTrackingName
        {
            get { return _selectedTrackingName; }
            set
            {
                _selectedTrackingName = value;
                NotifyPropertyChanged("SelectedTrackingName");
                if (value == null) {return;}
                App.Current.SelectedTrackingName = value;
                NavigateTo("/View/TrackingList_pg.xaml");
            }
        }

        private string _trackingItemName;
        public String TrackingItemName
        {
            get { return _trackingItemName; }
            set
            {
                _trackingItemName = value;
                NotifyPropertyChanged("TrackingItemName");
            }
        }

        private Visibility _trackingItemNameError;
        public Visibility TrackingItemNameError
        {
            get { return _trackingItemNameError; }
            set
            {
                _trackingItemNameError = value;
                NotifyPropertyChanged("TrackingItemNameError");
            }
        }

        #endregion

        #region Commanding

        #region Delete
        public ICommand DeleteCommand { get; private set; }
        public Boolean CanDelete(Object parameter)
        {
            return true;
        }

        private void btnDelete(Object parameter)
        {
            try
            {
                var res = parameter as TrackingName;

                if (Helpers.CrudTrackingName.DeleteTrackingName(res))
                    BindTrackingNameItems();
            }
            catch (Exception ex)
            {
                var msg = ex.Message;

            }
        } 
        #endregion

        #region Save

        public ICommand SaveCommand { get; private set; }
        public Boolean CanSave(Object parameter)
        {
            return true;
        }
        private void btnSave(Object parameter)
        {
            try
            {
                if (String.IsNullOrEmpty(TrackingItemName)) return;
                var trackingItem = new TrackingName { Name = TrackingItemName.ToLower(), DtRegister = DateTime.Now };
                var dt = (from res in GetTrackingNameItems() where res.Name == trackingItem.Name select res);
                if (dt.Any())
                {
                    TrackingItemNameError = Visibility.Visible;
                    return;
                }
                CrudTrackingName.AddTrackingName(trackingItem);
                BindTrackingNameItems();
                TrackingItemName = String.Empty;
                TrackingItemNameError = Visibility.Collapsed;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }
        }

        public ICommand RefreshCommand { get; private set; }
        public Boolean CanRefresh(Object parameter)
        {
            return true;
        }
        private void btnRefresh(Object parameter)
        {
            try
            {
                BindTrackingNameItems();
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }
        } 

        #endregion

        #region Select
        public ICommand SelectCommand { get; private set; }
        public Boolean CanSelect(Object parameter)
        {
            return true;
        }

        private void btnSelect(Object parameter)
        {
            try
            {
                SelectedTrackingName = parameter as TrackingName;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;

            }
        }
        #endregion

        #endregion
    }
}
